package com.nitty.cash.loop.common

import com.nitty.cash.loop.NittyApp
import com.nitty.cash.loop.base.BaseFragment
import com.nitty.cash.loop.event.NittyEventData
import com.nitty.cash.loop.event.NittyEventPointHelper
import com.nitty.cash.loop.event.NittyEventSubscriber
import com.nitty.cash.loop.request.HttpHelper
import com.nitty.cash.loop.request.IHttpCallback
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import org.json.JSONObject

/**
 * CommonFragment
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:32
 * @Annotation：
 */
abstract class CommonFragment: BaseFragment(), NittyEventSubscriber,ICommonView, CoroutineScope by MainScope() {

    override fun getAct(): CommonActivity {
        return context as CommonActivity
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.setTag(this)
        NittyApp.app.publisher.register(this)
    }

    /**
     * 客服
     */
    fun showCustomerDialog(eventRemark: String = "",pName: String = "", pToken: String = ""){
        getAct().showCustomerDialog(eventRemark,pName,pToken)
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        getAct().showErrorTipsDialog(tips,eventRemark)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String, params: JSONObject, callback: IHttpCallback){
        HttpHelper.post(httpUrl,params,callback)
    }

    /**
     * 添加埋点
     */
    fun addEventPointRequestAction(eventName: String, eventRemark: String = "") {
        NittyEventPointHelper.addEventPointRequestAction(eventName,eventRemark)
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: NittyEventData){
        NittyApp.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onNittyPublishEvent(event: NittyEventData) {
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.cancelTag(this)
        NittyApp.app.publisher.detach(this)
    }
}